package com.boomshake.controller;

import com.boomshake.common.PageResult;
import com.boomshake.common.Result;
import com.boomshake.dto.CommissionConfigDTO;
import com.boomshake.service.CommissionConfigService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 佣金配置管理控制器
 */
@RestController
@RequestMapping("/api/admin/commission")
public class CommissionConfigController {

    @Resource
    private CommissionConfigService commissionConfigService;

    /**
     * 分页查询佣金配置列表
     * @param storeId 门店ID，可为null
     * @param page 页码
     * @param size 每页数量
     * @return 分页结果
     */
    @GetMapping("/config/list")
    public Result<PageResult<CommissionConfigDTO>> getCommissionConfigList(
            @RequestParam(value = "storeId", required = false) Long storeId,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        PageResult<CommissionConfigDTO> pageResult = commissionConfigService.getCommissionConfigList(storeId, page, size);
        return Result.success(pageResult);
    }

    /**
     * 获取佣金配置详情
     * @param id 配置ID
     * @return 佣金配置
     */
    @GetMapping("/config/{id}")
    public Result<CommissionConfigDTO> getCommissionConfigById(@PathVariable("id") Long id) {
        CommissionConfigDTO config = commissionConfigService.getCommissionConfigById(id);
        if (config == null) {
            return Result.error("佣金配置不存在");
        }
        return Result.success(config);
    }

    /**
     * 新增佣金配置
     * @param config 佣金配置
     * @return 新增结果
     */
    @PostMapping("/config")
    public Result<Long> addCommissionConfig(@RequestBody CommissionConfigDTO config) {
        try {
            Long id = commissionConfigService.addCommissionConfig(config);
            return Result.success("新增佣金配置成功", id);
        } catch (Exception e) {
            return Result.error("新增佣金配置失败: " + e.getMessage());
        }
    }

    /**
     * 更新佣金配置
     * @param id 配置ID
     * @param config 佣金配置
     * @return 更新结果
     */
    @PutMapping("/config/{id}")
    public Result<Boolean> updateCommissionConfig(
            @PathVariable("id") Long id,
            @RequestBody CommissionConfigDTO config) {
        config.setId(id);
        try {
            boolean success = commissionConfigService.updateCommissionConfig(config);
            if (success) {
                return Result.success("更新佣金配置成功", true);
            } else {
                return Result.error("更新佣金配置失败，配置不存在");
            }
        } catch (Exception e) {
            return Result.error("更新佣金配置失败: " + e.getMessage());
        }
    }

    /**
     * 删除佣金配置
     * @param id 配置ID
     * @return 删除结果
     */
    @DeleteMapping("/config/{id}")
    public Result<Boolean> deleteCommissionConfig(@PathVariable("id") Long id) {
        try {
            boolean success = commissionConfigService.deleteCommissionConfig(id);
            if (success) {
                return Result.success("删除佣金配置成功", true);
            } else {
                return Result.error("删除佣金配置失败，配置不存在");
            }
        } catch (Exception e) {
            return Result.error("删除佣金配置失败: " + e.getMessage());
        }
    }

    /**
     * 获取门店所有等级的分销比例配置
     * @param storeId 门店ID
     * @return 分销比例配置列表
     */
    @GetMapping("/store/{storeId}/rates")
    public Result<List<CommissionConfigDTO>> getStoreCommissionRates(@PathVariable("storeId") Long storeId) {
        List<CommissionConfigDTO> configs = commissionConfigService.getStoreCommissionRates(storeId);
        return Result.success(configs);
    }

    /**
     * 批量更新门店的分销比例配置
     * @param storeId 门店ID
     * @param configs 分销比例配置列表
     * @return 更新结果
     */
    @PutMapping("/store/{storeId}/rates")
    public Result<Boolean> batchUpdateStoreCommissionRates(
            @PathVariable("storeId") Long storeId,
            @RequestBody List<CommissionConfigDTO> configs) {
        try {
            boolean success = commissionConfigService.batchUpdateStoreCommissionRates(storeId, configs);
            if (success) {
                return Result.success("批量更新佣金配置成功", true);
            } else {
                return Result.error("批量更新佣金配置失败");
            }
        } catch (Exception e) {
            return Result.error("批量更新佣金配置失败: " + e.getMessage());
        }
    }
} 